//
//  HMCustomAlertView.m
//  HMMarket
//
//  Created by haohao on 2017/6/7.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMCustomAlertView.h"

#define alertHeight 140
#define alertWidth  275
#define btnHeight   45
@interface HMCustomAlertView ()

@property (nonatomic, strong) UIView *alertView;

@property (nonatomic, strong) UIButton *cancleBtn;

@property (nonatomic, strong) UIButton *actionBtn;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, copy) NSString *titleString;

@property (nonatomic, copy) NSString *cancleString;

@property (nonatomic, copy) NSString *actionString;

@property (nonatomic, copy) NSString * contentString;


@end


@implementation HMCustomAlertView

- (instancetype)initWithTitle:(NSString *)title contentString:(NSString *)contentString cancleString:(NSString *)cancle actionString:(NSString *)action {
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        self.frame = CGRectMake(0, 0, KWidthSCREEN, KHeightSCREEN);
        _titleString = title;
        _cancleString = cancle;
        _actionString = action;
        _contentString = contentString;
        [self setUpSubview];
    }
    return self;
}

- (void)setUpSubview{
    _alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
    _alertView.center = self.center;
    _alertView.backgroundColor = [UIColor whiteColor];
    _alertView.layer.cornerRadius = 5;
    _alertView.clipsToBounds = YES;
    _alertView.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    _alertView.layer.shadowOffset = CGSizeMake(5, 5);
    _alertView.layer.shadowOpacity = 0.25;
    
    _alertView.layer.shadowColor = [UIColor colorWithHexString:@"000000"].CGColor;
    
    [self addSubview:_alertView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertWidth, 60)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont systemFontOfSize:18];
    _titleLabel.text = _titleString;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = kTextColor;
    [_alertView addSubview:_titleLabel];
    
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLabel.frame) - 20, alertWidth, 54)];
    _contentLabel.numberOfLines = 0;
    _contentLabel.backgroundColor = [UIColor whiteColor];
    _contentLabel.font = [UIFont systemFontOfSize:18];
    _contentLabel.text = _contentString;
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.textColor = kTextColor;
    [_alertView addSubview:_contentLabel];
    
    _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancleBtn.frame = CGRectMake(0, alertHeight - btnHeight, alertWidth / 2 - 1, btnHeight);
    [_cancleBtn setTitle:_cancleString forState:UIControlStateNormal];
    [_cancleBtn setTitleColor:[UIColor colorWithHexString:@"00994d"] forState:UIControlStateNormal];
    [_cancleBtn setBackgroundColor:[UIColor whiteColor]];
    [_cancleBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [_alertView addSubview:_cancleBtn];
    
    _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _actionBtn.frame = CGRectMake(alertWidth / 2, alertHeight - btnHeight, alertWidth / 2, btnHeight);
    [_actionBtn setTitle:_actionString forState:UIControlStateNormal];
    [_actionBtn setTitleColor:[UIColor colorWithHexString:@"00994d"] forState:UIControlStateNormal];
//    [_actionBtn setImage:[UIImage imageNamed:@"telephone"] forState:UIControlStateNormal];
    [_actionBtn setBackgroundColor:[UIColor whiteColor]];
    [_actionBtn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [_alertView addSubview:_actionBtn];
}

- (void)buttonClick {
    if ([_delegate respondsToSelector:@selector(alert:clickBnt:)]) {
        [_delegate alert:self clickBnt:ACTION];
    }
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.alpha = 0;
    self.alertView.transform = CGAffineTransformMakeScale(1.08, 1.08);
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.alertView.transform = CGAffineTransformIdentity;
    }];
}


- (void)dismiss {
    [UIView animateWithDuration:0.38 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
